﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LeadExpress.Models;
using LeadExpress.Models.ViewPages;

namespace LeadExpress.Repository
{
    public class BusinessTypeManagementRepository : BaseManagementRepository
    {
        LeadExpressEntities entities = new LeadExpressEntities();

        public BusinessTypeManagementModel GetModel(string pageType, string roleName, int id)
        {
            BusinessTypeManagementModel model = new BusinessTypeManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListBusinessType = GetAll();

            if (id > 0)
            {
                model.BusinessTypeInfo = GetSingle(id);
            }

            return model;
        }

        public BusinessTypeManagementModel GetModel(string pageType, string roleName)
        {
            BusinessTypeManagementModel model = new BusinessTypeManagementModel();
            model.ListBusinessType = GetAll();

            return model;
        }

        public List<BusinessType> GetAll()
        {            
            return (from c in entities.BusinessTypes
                         orderby c.DateCreated descending
                         select c).ToList();
        }

        public BusinessType GetSingle(int id)
        {
            return (from _object in entities.BusinessTypes where _object.ID == id select _object).FirstOrDefault();
        }

        public BusinessType GetSingle(string name)
        {
            return (from _object in entities.BusinessTypes where _object.Name == name select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] BusinessType _object)
        {
            _object.DateCreated = System.DateTime.Now;
            entities.AddToBusinessTypes(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.BusinessTypes where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void Save([Bind(Exclude = "ID")] BusinessType _object)
        {
            _object.EntityKey = (from BusinessType ac in entities.BusinessTypes where ac.ID == _object.ID select ac).FirstOrDefault().EntityKey;
            _object.DateCreated = System.DateTime.Now;
            entities.ApplyPropertyChanges(_object.EntityKey.EntitySetName, _object);
            entities.SaveChanges();
        }
    }
}